package com.neusoft.neuiotms.dcom.repository.insurance.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.enums.InsuranceTaskStatus;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceSummaryTask;
import com.neusoft.neuiotms.dcom.model.insurance.QInsuranceSummaryTask;
import com.neusoft.neuiotms.dcom.repository.insurance.CustomizedInsuranceSummaryTaskRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedInsuranceSummaryTaskRepositoryImpl extends AbstractRepository
        implements CustomizedInsuranceSummaryTaskRepository {

    @Override
    public List<InsuranceSummaryTask> index(final Searchable searchable) {
        final QInsuranceSummaryTask qInsuranceSummaryTask = QInsuranceSummaryTask.insuranceSummaryTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InsuranceSummaryTask> query = this.query().selectFrom(qInsuranceSummaryTask);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<InsuranceSummaryTask> search(final Pageable pageable, final Searchable searchable) {
        final QInsuranceSummaryTask qInsuranceSummaryTask = QInsuranceSummaryTask.insuranceSummaryTask;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<InsuranceSummaryTask> query = this.query().selectFrom(qInsuranceSummaryTask);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return InsuranceSummaryTask.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QInsuranceSummaryTask qInsuranceSummaryTask = QInsuranceSummaryTask.insuranceSummaryTask;

        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            // 编码检索
            where.and(super.equalsStr(qInsuranceSummaryTask.code, searchable, "code"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("reportPersonCode"))) {
            // 编码检索
            where.and(super.equalsStr(qInsuranceSummaryTask.reportPersonCode, searchable, "reportPersonCode"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("status"))) {
            // 状态检索
            where.and(super.equalsEnum(qInsuranceSummaryTask.status, searchable, "status",
                    value -> InsuranceTaskStatus.valueOf(value)));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("startTime"))) {
            where.and(
                    super.afterOrEqualsDate(qInsuranceSummaryTask.startTime, searchable, "startTime"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("endTime"))) {
            where.and(super.beforeOrEqualsDate(qInsuranceSummaryTask.endTime, searchable, "endTime"));
        }

        if (null != searchable.getStrValue("registerTaskId")) {
            // 总表任务id
            where.and(super.equalsLong(qInsuranceSummaryTask.registerTask.id, searchable, "registerTaskId"));
        }

        return where;
    }

}
